import 'package:flutter/material.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CountDown extends StatefulWidget {
  final int endTime;
  CountDown(
    this.endTime,
  );
  @override
  _CountDownState createState() => _CountDownState();
}

class _CountDownState extends State<CountDown> with TickerProviderStateMixin {
  AnimationController? _animationController;
  
  String get dayString {
    Duration duration = _animationController!.duration! * _animationController!.value;
    return '${(duration.inDays)..toString().padLeft(2, '0')}';
  }

  String get hoursString {
    Duration duration = _animationController!.duration! * _animationController!.value;
    return '${(duration.inHours)..toString().padLeft(2, '0')}';
  }
 
  String get minutesString {
    Duration duration = _animationController!.duration! * _animationController!.value;
    return '${(duration.inMinutes % 60).toString().padLeft(2, '0')}';
  }
 
  String get secondsString {
    Duration duration = _animationController!.duration! * _animationController!.value;
    return '${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
  }

  @override
  void dispose() {
    _animationController?.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    // int _tempTime = 86400000;
    // int _tempTime = 90055000;
    int _tempTime = widget.endTime * 1000;
    // 天数
    double _day = _tempTime / 1000 / 60 / 60 / 24;
    // // 小时
    double _hours = _tempTime / 1000 / 60 / 60 % 24;
    // 分钟
    double _minutes = _tempTime / 1000 / 60 % 60;
    // 秒
    double _seconds = _tempTime / 1000 % 60;
    _animationController = AnimationController(vsync: this, duration: Duration(
      days: _day.toInt(), 
      hours: _hours.toInt(), 
      minutes: _minutes.toInt(), 
      seconds: _seconds.toInt(), 
    ));
    _animationController!.reverse(from: _animationController!.value == 0.0 ? 1.0 : _animationController!.value);
    _animationController!.addStatusListener((status) {
      // if (_animationController!.status == AnimationStatus.dismissed) {
      //   widget.orderPageBloc.cleanOrderItem(widget.orderId);
      // }
    });
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animationController!,
      builder: (_, Widget? child) {
        // return Text(
        //   // '$dayString:$hoursString:$minutesString:$secondsString',
        //   '$hoursString:$minutesString:$secondsString',
        //   style: TextStyle(
        //     color: mainTextColor,
        //     fontSize: 14.sp
        //   ),
        // );
        return Container(
          margin: EdgeInsets.only(top: 2.h),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _timeItem(hoursString),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 3.w),
                child: Text(
                  ':',
                  style: TextStyle(
                    color: mainTextColor,
                    fontSize: 14.sp
                  ),
                ),
              ),
              _timeItem(minutesString),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 3.w),
                child: Text(
                  ':',
                  style: TextStyle(
                    color: mainTextColor,
                    fontSize: 14.sp
                  ),
                ),
              ),
              _timeItem(secondsString),
            ],
          ),
        );
      }
    );
  }

  Widget _timeItem(String time) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 3.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5.w),
        color: bgMainColor
      ),
      child: Text(
        '$time',
        style: TextStyle(
          color: textWhiteColor,
          fontSize: 10.sp
        ),
      ),
    );
  }
}